package com.dyz.wsw.domain.market.service.impl;

import com.dyz.wsw.common.pojo.page.PageData;
import com.dyz.wsw.domain.market.repository.persist.MarketCardRepository;
import com.dyz.wsw.domain.market.service.MarketService;
import com.dyz.wsw.domain.market.pojo.MarketCategory;
import com.dyz.wsw.domain.market.pojo.MarketCard;
import com.dyz.wsw.domain.market.repository.persist.MarketCategoryRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import java.util.*;

/**
 * @author dyz
 * Time 2024/6/9 20:02
 */
@Service
@Slf4j
public class MarketServiceImpl implements MarketService {

    private final MarketCardRepository marketCardRepository;


    private final MarketCategoryRepository marketCategoryRepository;


    @Autowired
    public MarketServiceImpl(MarketCardRepository marketCardRepository, MarketCategoryRepository marketCategoryRepository){
        log.info("创建市集服务对象:MarketServiceImpl");
        this.marketCardRepository = marketCardRepository;
        this.marketCategoryRepository = marketCategoryRepository;
    }

    @Override
    public Mono<PageData<MarketCard>> getCardsByCId(Long cId, Integer pageNum, Integer pageSize) {
        log.debug("开始处理【根据目录id查询所有模块】的数据访问，目录id：{},指定页数：{},请求数量：{}",cId,pageNum,pageSize);
        return marketCardRepository
                .getCardsByCId(cId,(pageNum-1)*pageSize,pageSize)
                .collectList()
                .map(list->{
                    PageData<MarketCard> pageData = new PageData<>();
                    pageData.setList(list);
                    pageData.setCurrentPage(pageNum);
                    pageData.setPageSize(list.size());
                    return pageData;
                });
    }

    @Override
    public Mono<List<MarketCategory>> findAllCategory() {
        log.debug("开始处理【从数据库查询市集分类目录信息】的数据访问，无参数");
        return marketCategoryRepository.findAll().collectList();
    }



    @Override
    public Mono<List<MarketCard>> getRandomCards(Integer count) {
        log.debug("开始处理【随机查找卡片】的数据访问，查找数量：{}",count);
        return marketCardRepository
                .getRandomCards((int)(Math.random()*10000),count)
                .collectList();
    }

}
